﻿using Platformer_Example.Controller.Character;
using Platformer_Example.Controller.Terrain;
using Platformer_Example.Screen.Playscreen;
using System;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Helper;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Platformer_Example.Controller.Map.Objects
{
    public abstract class BasePickable : BaseGravityAffected, IPickable
    {
        private readonly ImageContainer _hover;

        public override void MoveAbsolute(TE_Vector2 newPosition)
        {
            Position = newPosition;
        }

        public override void Move(TE_Vector2 position)
        {
            PositionGridBound = position;
        }

        public override void AddToAbsolutePosition(TE_Vector2 offset)
        {
            Position += offset;
        }

        protected override bool IsObject
        {
            get { return true; }
        }

        protected BasePickable()
        {
            AddChildren(GetObject());
            AddChildren(_hover = GetObjectHover());
            _hover.Visible = false;
        }

        protected abstract TE_Rectangle PickupBox { get; }

        public abstract BaseThrowable GetThrowableObject();
        public TE_Rectangle GetPickupCollisionArea()
        {
            return PickupBox + AbsolutePosition;
        }

        public override TE_Vector2 GetAbsolutePosition()
        {
            return AbsolutePosition;
        }

        protected abstract Container GetObject();
        protected abstract ImageContainer GetObjectHover();

        public override void FixedStepUpdate(TimeSpan gameTime)
        {
            if (CollisionHelper.Intersects(Playscreen.LocalPlayer.GetPickupCollisionArea(), GetPickupCollisionArea()) && !Playscreen.LocalPlayer.IsCarryingObject)
            {
                _hover.Visible = true;
                _hover.Color = new Color(Playscreen.LocalPlayer.Team.GetColor(), 100);
            }
            else
            {
                _hover.Visible = false;
            }

            ApplyGravity(gameTime);
            ApplyMovementX(gameTime);
            if(ApplyMovementY(gameTime, true) == BaseTerrain.TerrainType.Floor)
                ResetGravityY();
            
            ApplyWarps();

            base.FixedStepUpdate(gameTime);
        }
    }
}